using System;
using System.Collections.Generic;
using System.Text;
using BoOnce.GameLogic.Entities;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Collisions;
using BoOnce.Helpers;

namespace BoOnce.Components
{
    class CameraConfig
    {
        #region Attributes
        public float MinX;
        public float MaxX;
        public float MinY;
        public float MaxY;

        public float CameraWidth;
        public float CameraHeight;
        #endregion

        #region Constructor
        public CameraConfig()
        {
            MinX = 0;
            MaxX = Settings.SCREEN_X;
            MinY = 0;
            MaxY = Settings.SCREEN_Y;

            CameraHeight = Settings.SCREEN_Y;
            CameraWidth = Settings.SCREEN_X;
        }

        public CameraConfig(float minX, float maxX, float minY, float maxY)
        {
            MinX = minX;
            MaxX = maxX;
            MinY = minY;
            MaxY = maxY;

            CameraHeight = Settings.SCREEN_Y;
            CameraWidth = Settings.SCREEN_X;
        }

        public CameraConfig(float minX, float maxX, float minY, float maxY, float camx, float camy)
        {
            MinX = minX;
            MaxX = maxX;
            MinY = minY;
            MaxY = maxY;

            CameraHeight = camy;
            CameraWidth = camx;
        }
        #endregion
    }

    class CameraComponent : GameComponent
    {
        public Somebody FollowedEntity;
        private Vector2 _CameraTranslation;

        private CameraConfig _Config;

        public Vector2 CameraTranslation
        {
            get
            {
                return _CameraTranslation;
            }
        }

        public override void Initialize()
        {
            Logger.WriteInfo("CameraComponent[#" + this.UpdateOrder + "] init");
            base.Initialize();
        }

        public CameraComponent(Game game, CameraConfig config)
            : base(game)
        {
            FollowedEntity = null;
            _Config = config;
            _CameraTranslation = new Vector2();
        }

        public void SetCameraLimit(float minX, float maxX, float minY, float maxY)
        {
            _Config.MinX = minX;
            _Config.MaxX = maxX;
            _Config.MinY = minY;
            _Config.MaxY = maxY;
        }

        public void SetConfig(CameraConfig config)
        {
            _Config = config;
        }

        public Vector2 ToScreenPosition(Vector2 worldPosition)
        {
            return new Vector2(worldPosition.X - _CameraTranslation.X, worldPosition.Y - _CameraTranslation.Y);
        }

        public override void Update(GameTime gameTime)
        {
            float camX = 0;
            float camY = 0;

            if (FollowedEntity != null)
            {
                camX = FollowedEntity.Position.X - _Config.CameraWidth / 2.0f;
                camY = FollowedEntity.Position.Y - _Config.CameraHeight / 2.0f;
                if (camX < _Config.MinX)
                {
                    camX = _Config.MinX;
                }

                if (camX > _Config.MaxX)
                {
                    camX = _Config.MaxX;
                }

                if (camY < _Config.MinY)
                {
                    camY = _Config.MinY;
                }

                if (camY > _Config.MaxY)
                {
                    camY = _Config.MaxY;
                }

            }
            _CameraTranslation.X = camX;
            _CameraTranslation.Y = camY;
        }

    }
}
